/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.uitls;

import ac.settings.dataModelurls;
import ac.settings.files;
import java.awt.ComponentOrientation;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *The class that handles the properties file. the operations include reading and writing properties.
 * The methods in this class can use predefined paths for the properties file or use a new one from method parameter.
 * @author ismail
 */
public class propertiesOperation implements dataModelurls, files {

    Properties prop = new Properties();
/**
 * finds the value of a given property key using the given url to get the property file from inside the jar file.
 * @param url the url of the file
 * @param name the property key to get its value
 * @return the value of the given property key
 */
    private String getWordFromProperty(String url, String name) {
        String returnData;

        try {
            Properties props = new Properties();

            props.load(this.getClass().getResourceAsStream(url));
            returnData = props.getProperty(name);

        } catch (IOException ex) {
            returnData = ex.getMessage();

        }

        return returnData;

    }
/**
 * finds the value of a given property key using the given url to get the property file from a file in the machine file system.
 * @param url the url of the file
 * @param name the property key to get its value
 * @return the value of the given property key
 */
    private String getWordFromPropertyFixPath(String url, String name) {
        String returnData;

        try {
            Properties props = new Properties();

            props.load(new FileInputStream(url));
            returnData = props.getProperty(name);

        } catch (IOException ex) {
            returnData = ex.getMessage();

        }

        return returnData;

    }
/**
 * Set the value of a given property inside a property file exists in the machine's file system as the given url.
 * @param url the location of the property file
 * @param name the property name to set its value
 * @param value the new value to set
 * @return error message if any
 * @throws URISyntaxException 
 */
    private String setWordFromPropertyFixpath(String url, String name, String value) throws URISyntaxException {

        String returnData = "";

        ///  String propsFileName = url;
        try {

            prop.load(new FileInputStream(url));

            File f = new File(url);

            OutputStream out = new FileOutputStream(f);
            prop.setProperty(name, value);
            prop.store(out, "This description goes to the header of a file");
            // uiUtil.sendMessage(name);
            out.close();

        } catch (IOException ex) {
            returnData = ex.getMessage();
        }

        return returnData;

    }

    private String setWordFromProperty(String url, String name, String value) throws URISyntaxException {

        String returnData = "";

        ///  String propsFileName = url;
        try {
            prop.load(this.getClass().getResourceAsStream(url));
            File f = new File(this.getClass().getResource(url).toURI());
            OutputStream out = new FileOutputStream(f);
            prop.setProperty(name, value);
            prop.store(out, "This description goes to the header of a file");
            // uiUtil.sendMessage(name);
            out.close();

        } catch (IOException ex) {
            returnData = ex.getMessage();
        }

        return returnData;

    }
/**
 * Set the value of a given property inside a property file using the default path.
 * @param name the property name to set its value
 * @param value the new value to set
 * @return error message if any
 */
    public String setProperty(String name, String value) {

        String returnData = "";
        try {
            returnData += this.setWordFromPropertyFixpath(homeConfigPath + propertiesFileName, name, value);
        } catch (URISyntaxException ex) {
            Logger.getLogger(propertiesOperation.class.getName()).log(Level.SEVERE, null, ex);
        }
        return returnData;
    }
/**
 * finds the value of the given property using the default path for the property file
 * @param name the property to get its value
 * @return the value of the property
 */
    public String getProperty(String name) {

        String returnData;
        returnData = this.getWordFromPropertyFixPath(homeConfigPath + propertiesFileName, name);
        return returnData;
    }
/**
 * Gets the properties as an object from the default properties file
 * @return the properties object
 */
    public Properties getLocalProperty() {
        Properties props = new Properties();
        try {

            String returnData;

            props.load(this.getClass().getResourceAsStream(RESOURCES_URL + "general.properties"));

        } catch (IOException ex) {
            Logger.getLogger(propertiesOperation.class.getName()).log(Level.SEVERE, null, ex);
        }
        return props;
    }
/**
 * gets the value of property according to the current language settings
 * @param name the property name to get its value
 * @return the value of the property
 */
    public String getWord(String name) {
        String returnData = "";
        String lang = this.getProperty("lang");
        returnData = this.getWordFromProperty(RESOURCES_URL + lang + ".properties", name);

        if (returnData == null || returnData.trim().equals("")) {
            returnData = name.replace("label.", "");

        }
        return returnData;
    }
/**
 * finds the value of the given property using the default path for the property file
 * @param name the property to get its value
 * @return the value of the property
 */
    public String getPropertys(String name) {

        String returnData;
        returnData = this.getWordFromProperty(RESOURCES_URL + "general.properties", name);
        return returnData;
    }
/**
 * gets the value of property according to the current language settings
 * @param name the property name to get its value
 * @return the value of the property
 */
    public String getWords(String name) {
        String returnData = "";
        String lang = this.getProperty("lang");
        returnData = this.getWordFromProperty(RESOURCES_URL + lang + ".properties", name);

        if (returnData == null || returnData.trim().equals("")) {
            returnData = name.replace("label.", "");

        }
        return returnData;
    }
/**
 * Get the component orientation according to the current language selected
 * @return the orientation to be used
 */
    public Object getComponentOrientation() {
        Object retu = null;
        if (this.getProperty("lang").equalsIgnoreCase("ar")) {
            retu = ComponentOrientation.RIGHT_TO_LEFT;
        }

        return retu;

    }
}
